package com.eling.elcms.travel.service.impl;

import com.eling.elcms.basedoc.model.BusinessMessage;
import com.eling.elcms.basedoc.model.PersonalInfo;
import com.eling.elcms.basedoc.model.TargetType;
import com.eling.elcms.basedoc.model.TaskType;
import com.eling.elcms.basedoc.service.IBusinessMessageManager;
import com.eling.elcms.basedoc.service.IPersonalInfoManager;
import com.eling.elcms.core.AppContext;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.ordermanager.model.ServicePoint;
import com.eling.elcms.ordermanager.service.IServicePointManager;
import com.eling.elcms.system.model.CommonUser;
import com.eling.elcms.travel.dao.IPlanItemDao;
import com.eling.elcms.travel.model.*;
import com.eling.elcms.travel.model.Customer.DataSource;
import com.eling.elcms.travel.model.PlanItemGroup.SignUpStatus;
import com.eling.elcms.travel.model.view.AlliancePlanItemGridView;
import com.eling.elcms.travel.model.view.AlliancePlanItemSaveView;
import com.eling.elcms.travel.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class PlanItemManagerImpl extends GenericManagerImpl<PlanItem, Long> implements IPlanItemManager {

	IPlanItemDao itemDao;
	
	@Autowired
	private ICustomerManager customerManager;

	@Autowired
	private ISignUpRegisterManager signUpRegisterManager;

	@Autowired
	private ISignUpChargeManager signUpChargeManager;

	@Autowired
	private IPlanItemGroupManager planItemGroupManager;
	@Autowired
 	private IBusinessMessageManager businessMessageManager;
	@Autowired
	private IPersonalInfoManager personalInfoManager;
	@Autowired
	private IServicePointManager servicepointmanager;
	@Autowired
	public void setItemDao(IPlanItemDao itemDao) {
		this.dao = itemDao;
		this.itemDao = itemDao;
	}

	@Override
	public List<PlanItem> inquirySignup(List<PlanItem> list) {
		return itemDao.inquirySignup(list);
	}

	@Override
	public PlanItem updata(PlanItem item, Long pkServicePoint) {
		ServicePoint sp=servicepointmanager.get(pkServicePoint);
		PlanItem plan = itemDao.get(item.getPkPlanItem());
		plan.setReceivables(item.getReceivables());
		plan.setVersion(item.getVersion());

		//报名登记确认时推送收费单
		PlanItem planItem = super.save(plan);
		
		SignUpRegister signUpRegister = new SignUpRegister();
		PlanItemGroup planItemGroup = new PlanItemGroup();
		planItemGroup.setServicePoint(sp);
		planItemGroup.setPlanItem(plan);
		List<PlanItemGroup> list=planItemGroupManager.query(planItemGroup);
		if(list.size()>0){
			list.get(0).setPayable(plan.getReceivables());
			list.get(0).setSignUpStatus(PlanItemGroup.SignUpStatus.Confirm);
			planItemGroup=planItemGroupManager.save(list.get(0));
		}
		signUpRegister.setPlanItemGroup(planItemGroup);
		List<SignUpRegister> surList = signUpRegisterManager.query(signUpRegister);
		if (!surList.isEmpty()) {
			List<SignUpCharge> sucList = new ArrayList<SignUpCharge>();
			for (SignUpRegister sur : surList) {
				SignUpCharge suc = new SignUpCharge();
				suc.setSignUpRegister(sur);
				suc.setVersion(0);
				sucList.add(suc);
			}
			signUpChargeManager.save(sucList);
			businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.SignUpCharge,planItemGroup.getPkPlanItemGroup()));
		}

		return planItem;
	}

	@Override
	public void saveAlliancePlanItem(AlliancePlanItemSaveView view) {
		//删除所有编辑时保存时的数据
		if (null != view.getPkSignList() && !view.getPkSignList().isEmpty()) {
			SignUpRegister sur = new SignUpRegister();
			PropertyUtils.setProperty(sur, "pkSignUpRegisterIn", view.getPkSignList());
			PropertyUtils.setProperty(sur, "validatePrivilege", false);
			List<SignUpRegister> surList = signUpRegisterManager.query(sur);
			List<Long> pkPerList = surList.stream().map(a->a.getPersonalInfo().getPkPersonalInfo()).collect(Collectors.toList());
			Customer customer = new Customer();
			PropertyUtils.setProperty(customer, "personalInfo.pkPersonalInfoIn", pkPerList);
			PropertyUtils.setProperty(customer, "validatePrivilege", false);
			List<Customer> cusList = customerManager.query(customer);
			PersonalInfo per = new PersonalInfo();
			PropertyUtils.setProperty(per, "pkPersonalInfoIn", pkPerList);
			List<PersonalInfo> perList = personalInfoManager.query(per);
			//删除客户
			customerManager.remove(cusList);
			//删除登记
			signUpRegisterManager.remove(surList);
			//删除人员信息
			personalInfoManager.remove(perList);
		}
		
		PlanItemGroup planItemGroup = view.getPlanItemGroup();
		planItemGroup.setAllianceOrg(planItemGroup.getPlanItem().getAllianceOrg());
		planItemGroup.setAlliance(planItemGroup.getPlanItem().getAlliance());
		planItemGroup.setSignUpStatus(SignUpStatus.Confirm);
		planItemGroup.setIsAdjustment(false);
		
		//保存房间信息
		if(view.getSignUpRoomTypeCounts() != null){
			Set<SignUpRoomTypeCount> signUpRoomTypeCountSet = new HashSet<>(view.getSignUpRoomTypeCounts());
			planItemGroup.setSignUpRoomTypeCounts(signUpRoomTypeCountSet);
		}
		
		planItemGroup = planItemGroupManager.save(planItemGroup);
		businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.GroupReserve,planItemGroup.getPkPlanItemGroup()));

		List<SignUpRegister> sucList = view.getSignUpList();
		
		if (sucList!=null&&!sucList.isEmpty()) {
			// 保存登记信息
			for (SignUpRegister sur : sucList) {
				if(sur.getPlanItemGroup() == null){
					sur.setPlanItemGroup(planItemGroup);
				}
			}
			List<SignUpRegister> sucSaveList = signUpRegisterManager.save(sucList);
		
			planItemGroup.setSignUps(new HashSet<>(sucSaveList));
			
			planItemGroupManager.save(planItemGroup);

			// 抽取未保存的人员信息
			List<PersonalInfo> perList = new ArrayList<PersonalInfo>();
			for (SignUpRegister sur1 : sucList) {
				if (null == sur1.getPersonalInfo().getPkPersonalInfo()) {
					for (SignUpRegister sur2 : sucSaveList) {
						if (sur1.getPersonalInfo().getIdNumber().equals(sur2.getPersonalInfo().getIdNumber())) {
							perList.add(sur2.getPersonalInfo());
						}
					}
				}
			}

			// 保存客户
			List<Customer> cusList = new ArrayList<Customer>();
			CommonUser saleConsultant = (CommonUser)AppContext.curUser();
			for (PersonalInfo per : perList) {
				Customer cus = new Customer();
				per.setVersion(0);
				cus.setPersonalInfo(per);
				cus.setOrganization(((CommonUser) AppContext.curUser()).getOrganization());
				cus.setDataSource(DataSource.Alliance);
				cus.setVersion(0);
				cus.setSaleConsultant(saleConsultant);
				cusList.add(cus);
			}
			if (!cusList.isEmpty()) {
				cusList = customerManager.save(cusList);
			}
		}
	}

	@Override
	public List<AlliancePlanItemGridView> queryAlliancePlanItem(AlliancePlanItemGridView view) {
		List<AlliancePlanItemGridView> returnList = new ArrayList<AlliancePlanItemGridView>();
		List<PlanItemGroup> planItemGroupList = planItemGroupManager.query(view.getPlanItemGroup());
		if (!planItemGroupList.isEmpty()) {
			List<Long> pkList = planItemGroupList.stream().map(a -> a.getPkPlanItemGroup())
					.collect(Collectors.toList());
			SignUpRegister sur = new SignUpRegister();
			PropertyUtils.setProperty(sur, "planItemGroup.pkPlanItemGroupIn", pkList);
			PropertyUtils.setProperty(sur, "validatePrivilege", false);
			List<SignUpRegister> surList = signUpRegisterManager.query(sur);
			for (PlanItemGroup pig : planItemGroupList) {
				AlliancePlanItemGridView apigv = new AlliancePlanItemGridView();
				apigv.setPlanItemGroup(pig);
				Integer signUpNumber = 0;
				if (!surList.isEmpty()) {
					for (SignUpRegister sur2 : surList) {
						if (sur2.getPlanItemGroup().getPkPlanItemGroup().equals(pig.getPkPlanItemGroup())) {
							signUpNumber += 1;
						}
					}
				}
				apigv.setSignUpNumber(signUpNumber);
				returnList.add(apigv);
			}
		}
		return returnList;
	}
}
